package org.six.background.service.impl;

import org.six.background.config.ImitateRedis;
import org.six.background.mapper.UserMapper;
import org.six.background.pojo.User;
import org.six.background.pojo.UserExample;
import org.six.background.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired
    private ImitateRedis imitateRedis;

    @Override
    //登录
    public Map logintest(User user){
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andUsnameEqualTo(user.getUsname());
        criteria.andUspassEqualTo(user.getUspass());
        List<User> users=userMapper.selectByExample(userExample);
        String uuid=UUID.randomUUID().toString();
        String token="user"+uuid;
        Map toke=new HashMap();
        toke.put("user",users.get(0));
        toke.put("token",token);
        toke.put("oldTime",new Date().getTime());
        imitateRedis.getLoginMap().put(users.get(0).getUsid().toString(),toke);
        System.out.println(imitateRedis.getLoginMap().toString());
        if (!users.isEmpty()){
            Map result =new HashMap();
            result.put("usid",users.get(0).getUsid());
            result.put("token",token);
            return result;
        } else{
            return null;
        }
    }

    @Override
    public User selectById(Integer usid){
        return userMapper.selectByPrimaryKey(usid);
    }
}
